package cn.edu.sziit.bbs.controller;

import cn.edu.sziit.bbs.model.Post;
import cn.edu.sziit.bbs.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class PostSeekController {

    @Autowired
    PostService postService;

    @RequestMapping(value = "/postSeek", method = RequestMethod.GET)
    public String postSeekShow() {
        return "postSeek";
    }

    @ResponseBody
    @RequestMapping(value = "/postSeek", method = RequestMethod.POST, produces = "application/json;" +
            "charset=UTF-8")
    public Map<String, String> selectAllByPostTitle(String postTitle, HttpSession session) {
        Map<String, String> map = new HashMap<>();

        List<Post> postsSeek = postService.selectAllByPostTitle(postTitle);
//        List<Post> seekPosts = null;

//        if (number<=0){
//            map.put("info", "页数有误");
//            return map;
//        }

//        if (seekPostsAll.size()>=6*number){
//            seekPosts=seekPostsAll.subList(6*(number-1), 6*number);
//
//        }else if(seekPostsAll.size()>=6*(number-1)&&seekPostsAll.size()<6*number){
//            seekPosts=seekPostsAll.subList(6*(number-1), seekPostsAll.size());
//        }else{
//            map.put("info", "页数有误");
//            return map;
//        }

        System.out.println(postsSeek.size());
        session.setAttribute("postsSeek", postsSeek);
//        session.setAttribute("seekPostPage", number);

        map.put("info", "success");

        return map;
    }

}
